// $codepro.audit.disable unnecessaryCast
/**
 * Project: Utilities <br>
 * Package: org.leumasjaffe.functional.lazy
 */
package org.leumasjaffe.functional.lazy;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.leumasjaffe.functional.Predicate;

/**
 * Class org.leumasjaffe.functional.lazy..LazyListFilter in file LazyListFilter.java <br>
 * 
 * @author leumasjaffe
 * @version Nov 28, 2013
 * Created on: Nov 28, 2013
 * 
 * @param <T>
 * @param <B>
 */
@NonNullByDefault
class LazyFilter<T, B> extends Lazy<T, B> 
implements StateAdvance<T, T> {
	private final Predicate<? super T> criteria;

	/**
	 * @param root
	 * @param criteria
	 */
	LazyFilter(LazyRoot<B> root, Predicate<? super T> criteria) {
		super(root);
		this.criteria = criteria;
	}

	/* (non-Javadoc)
	 * @see org.leumasjaffe.functional.lazy.StateAdvance#advanceState(java.lang.Object)
	 */
	@Override
	public @Nullable T advanceState(T t) {
		return this.criteria.test(t) ? t : null;
	}
	
	/* (non-Javadoc)
	 * @see org.leumasjaffe.functional.lazy.StateAdvance#advanceAllStates(java.lang.Object)
	 */
	@SuppressWarnings("null")
	@Override
	@Nullable
	public
	<O> O advanceAllStates(T t) {
		final @Nullable T r = this.advanceState(t);
		return (O) (r == null ? null : this.next == null ? r : this.next.advanceAllStates(r));
	}

}
